package cn.china.myspringboot.config;

import cn.china.myspringboot.entity.Result;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class NoneRolePermissionException {
    //登录认证异常
    @ExceptionHandler( {AuthenticationException.class})
    public void authenticationException(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("info","登录失败");
        response.getWriter().println(Result.error(jsonObject));
    }

    //无操作权限的处理
    @ExceptionHandler( { UnauthorizedException.class, AuthorizationException.class, AuthorizationException.class,UnauthorizedException.class  } )
    public void authorizationExCeption(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("info","无操作权限");
        response.getWriter().println(Result.error(jsonObject));
    }

}